package com.ben.multiple.shard.springjdbc;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ben.multiple.shard.springjdbc.dto.ModelVO;
import com.ben.multiple.shard.springjdbc.mp.Book;
import com.ben.multiple.shard.springjdbc.mp.BookMapper;
import com.ben.multiple.shard.springjdbc.mp.Model;
import com.ben.multiple.shard.springjdbc.mp.ModelDetail;
import com.ben.multiple.shard.springjdbc.mp.ModelDetailMapper;
import com.ben.multiple.shard.springjdbc.mp.ModelMapper;
import com.ben.multiple.shard.springjdbc.mp.Order;
import com.ben.multiple.shard.springjdbc.mp.OrderMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
    static AtomicInteger atomicInteger = new AtomicInteger(LocalDateTime.now().getSecond());
    static AtomicLong atomicLong = new AtomicLong(LocalDateTime.now().getMinute());
    static String[] cs = {"A", "B", "C"};

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private BookMapper bookMapper;
    @Resource
    private ModelMapper modelMapper;
    @Resource
    private ModelDetailMapper modelDetailMapper;

    @GetMapping("/insertOrder")
    public Map<String, Object> insertOrder() {
        List<Order> list = new ArrayList<>();

        list.add(new Order(
                null, new Random().nextInt(2), atomicInteger.getAndIncrement(),
                atomicLong.getAndIncrement(), cs[new Random().nextInt(3)]));

        list.add(new Order(
                null, new Random().nextInt(2), atomicInteger.getAndIncrement(),
                atomicLong.getAndIncrement(), cs[new Random().nextInt(3)]));

        for (Order obj : list) {
            orderMapper.insert(obj);
            log.info("insertOrder, obj : {}", obj);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("all", list);
        return map;
    }

    @GetMapping("/insertBook")
    public Map<String, Object> insertBook() {
        List<Book> list = new ArrayList<>();
        list.add(new Book((Integer) null, (long) (new Random().nextInt(10) + 1996), "张三丰传记", "刘伯温", "。。。"));
        list.add(new Book((Integer) null, (long) (new Random().nextInt(10) + 1996), "刘德华传记", "徐前", "。。。"));
        for (Book obj : list) {
            bookMapper.insert(obj);
            log.info("insertBook, obj: {}", obj);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("all", list);
        return map;
    }

    @GetMapping("/bindInsert")
    public Map<String, Object> bindInsert() {
        List<Model> modelList = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            Model model = new Model(null, "模型-" + (i + 1));
            modelList.add(model);
        }
        for (Model obj : modelList) {
            modelMapper.insert(obj);
        }
        List<Model> allModel = modelMapper.selectList(Wrappers.emptyWrapper());
        List<ModelDetail> allModelDetail = modelDetailMapper.selectList(Wrappers.emptyWrapper());
        List<Long> modelIdList = allModel.stream().map(Model::getModelId).toList();
        Set<Long> existModelIdSet = allModelDetail.stream().map(ModelDetail::getModelId).collect(Collectors.toSet());
        List<Long> notExistModelIdList = modelIdList.stream().filter(obj -> !existModelIdSet.contains(obj)).toList();
        List<ModelDetail> modelDetailList = new ArrayList<>();
        for (Long obj : notExistModelIdList) {
            ModelDetail modelDetail = new ModelDetail(null, obj, "啦啦啦-");
            modelDetailList.add(modelDetail);
        }
        for (ModelDetail obj : modelDetailList) {
            modelDetailMapper.insert(obj);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("model", modelList);
        map.put("modelDetail", modelDetailList);
        return map;
    }

    @GetMapping("/bindSelect")
    public Map<String, Object> bindSelect(){
        List<ModelVO> modelVOS = modelMapper.queryBind();
        HashMap<String, Object> map = new HashMap<>();
        map.put("modelVOS", modelVOS);
        return map;
    }


}
